<?php
session_start();
include 'inc/db_connect.php';
include 'inc/db_op.php';
include 'inc/html_op.php';

$page_title = 'GetPassword';

function get_form() {
  $html_str = "";
  $html_str .= '<form name="getpwd" method="post">请输入邮箱地址：<input type="email" name="email" size=40></input><button type="submit" name="submit">发送</button></form>';
  return $html_str;
}

function get_content() {
  $html_str = "";
  if(isset($_POST["submit"])){
    $email=$_POST["email"];
//  echo '输入的地址是:'.$email;

    $sql = "select name, password from user_info where email like '$email'";
    $rows = db_get($sql);
    
    if(count($rows) > 0){
      $row = $rows[0];
      $name=$row["name"];
      $pwd=$row["password"];
      $subject=$name.'通行字';
      $body='通行字：'.$pwd.'。';
      $header='from:gongchengming@sgepri.sgcc.com.cn';//\r\nCC:gongchm@gmail.com\r\nContent-type:text/html;charset=utf-8';
//    echo '<br/>'.$email.','.$subject.','.$body.'<br/>';
      mail($email, $subject, $body, $header);
      $html_str .= '请稍后检查主题为"'.$subject.'"的邮件，邮件为utf-8编码。若收件箱中没有，注意检查垃圾邮件。<a href="login.php">登录</a>';
    } else {
      $html_str .= "<p>你输入的邮箱".$email."不是部门成员的工作邮箱，请重输。</p>".get_form();
    }
  } else {
    $html_str .= get_form();
  }
  return $html_str;
}

$content = get_content();
$login_required = 0;
echo gen_onecol_page($page_title, $content, $debug, $login_required);
?>
